//
//  RegisterViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/18.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "RegisterViewController.h"
#import "NetworkTool.h"
#import "NSString+MD5.h"


#import "LoginAndRegisterModel.h"
#import "LoginAndRegisterData.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UIButton *completeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *passWordText;

@property (nonatomic,strong)LoginAndRegisterModel *registerModel;
@property (nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataArray = [NSMutableArray array];
    _codeButton.clipsToBounds = YES;
    _codeButton.layer.cornerRadius = self.codeButton.frame.size.height / 2;
    _completeButton.clipsToBounds = YES;
    _completeButton.layer.cornerRadius = self.completeButton.frame.size.height / 2;
    
}
- (IBAction)handleBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)codeButton:(id)sender {
    [self handleCodeData];
    
}

//

- (IBAction)completeButton:(id)sender {
    
    [self handleRegistData];
}

- (void)handleRegistData{
    
    NSString *urlString = @"http://login.5wuli.com/v1/user/register";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"login.5wuli.com", @"Content-Type":@"application/json;charset=utf-8"};
    
//    NSDictionary *body = @{@"mobile":_phoneTextField.text, @"pwd":md5String};
    NSString *md5String = [_passWordText.text stringWith32BitMD5Lower];

//    NSDictionary *body = @{@"code":_codeText.text, @"mobile":_phoneText.text, @"pwd":md5String};
    
    NSString *str1 = @"{\"code\": \"";
    NSString *str2 = @"\",\"mobile\": \"";
    NSString *str3 = @"\",\"pwd\": \"";
    NSString *str4 = @"\"}";
    
    NSString *bodyString = [[[[[[str1 stringByAppendingString:_codeText.text] stringByAppendingString:str2] stringByAppendingString:_phoneText.text] stringByAppendingString:str3] stringByAppendingString:md5String] stringByAppendingString:str4];
    
    [NetworkTool POST:urlString andBody:bodyString andBodyStyle:JHBodyString andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        NSLog(@"------------------%@",result);
        
        _registerModel = [LoginAndRegisterModel modelWithDic:result];
        [_dataArray addObject:_registerModel];
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *imageUrl = [userDefaults objectForKey:@"userImageUrl"];
        if (imageUrl) {

            
        }else{
            [userDefaults setObject:_registerModel.data.nickName forKey:@"userName"];
            [userDefaults setObject:_passWordText.text forKey:@"password"];
            [userDefaults setObject:_registerModel.data.portrait forKey:@"userImageUrl"];
            
            [userDefaults synchronize];


        }

        
        
    } andFailureBlock:^(NSError *error) {
        
        NSLog(@"请求失败");
        
    }];

}
//注册请求下来的数据
//{
//    "requestId": "be6ff5cb-e764-43c6-b86a-22379c950017",
//    "requestStartTime": 1474340214602,
//    "requestEndTime": 1474340214602,
//    "code": "10000",
//    "data": {
//        "uId": "b926855ce56b772f856b67d6b35ac9a4",
//        "credential": "e1MyMDIwMDAyMDJ9LXsjIyMjIyMjIyMjIyMjIyMjIyNiODAxM2VkNmY5MDFlYmE2MWZlMDU0ZTM1MmM5MTcwM30teyMjIyMjIyMxNDc0MzQwMjE0NTkwLWRlMjFmZDNkMzJ9QDE0NzM1MjYyODAwMDU=",
//        "nickName": "18242365028",
//        "portrait": "http://meta.9liuda.com/image/user/icon/center_default_icon01.png",
//        "mobile": "18242365028",
//        "sex": null,
//        "birthday": null,
//        "appId": null,
//        "createTime": null,
//        "lastLoginTime": 1474340215000,
//        "totalLoginCount": 1,
//        "partnerInfoMap": null,
//        "ageGroup": null,
//        "profession": null
//    },
//    "message": "ok",
//    "attached": null
//}



//15998571702




- (void)handleCodeData{
    
    NSString *head = @"http://login.5wuli.com/v1/basic/authcode?mobile=";
    NSString *tail = @"&check=true";
    NSString *urlString = [[head stringByAppendingString:_phoneText.text]stringByAppendingString:tail];
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"login.5wuli.com", @"Content-Type":@"application/json;charset=utf-8"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",result);
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
